//
//  HomeManager.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "HomeManager.h"
#define Base_URL @"http://api.breadtrip.com/destination/v3/?last_modified_since=0"
#import "HomeModel.h"

@implementation HomeManager

+(HomeManager *)sharedHomeManager{
    static HomeManager *manager = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HomeManager alloc]init];
        [manager requesetNetWork];

    });

    return manager;
}

-(void)requesetNetWork{
    NSURL *url = [NSURL URLWithString:Base_URL];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",data);
        NSLog(@"%@",error);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        self.dataArray = [NSMutableArray arrayWithCapacity:4];




        for (NSDictionary *dict in dic[@"banners"]) {
            HomeModel * homeModel = [[HomeModel alloc] init];

            [homeModel setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:homeModel];

        }

        dispatch_async(dispatch_get_main_queue(), ^{
            self.updateUI();
        });



    }];

    [task resume];

}


-(HomeModel *)returnModelByIndex:(NSInteger)index{
    return [self.dataArray objectAtIndex:index];
}


@end
